<!DOCTYPE html><html lang="zh-CN,en,default"><head hexo-theme="https://github.com/volantis-x/hexo-theme-volantis/tree/4.3.1"><meta charset="utf-8"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><meta name="renderer" content="webkit"><meta name="force-rendering" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"><meta name="HandheldFriendly" content="True"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><link rel="preload" href="/css/first.css" as="style"><title>Nginx学习笔记 - Tang&#39;s blog.</title><meta name="keywords" content="Nginx"><meta name="description" content="Nginx主要用于1. 静态资源服务器，2. 反向代理，3. 负载均衡，本文主要围绕这三块进行描述。"><link rel="alternate" href="/atom.xml" title="Tang's blog." type="application/atom+xml"><meta name="msapplication-TileColor" content="#ffffff"><meta name="msapplication-config" content="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/blog/favicon/browserconfig.xml"><meta name="theme-color" content="#ffffff"><link rel="apple-touch-icon" sizes="180x180" href="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/favicon/cf_1o_180x180.png"><link rel="icon" type="image/png" sizes="32x32" href="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/favicon/cf_1o_32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/favicon/cf_1o_16x16.png"><link rel="manifest" href="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/favicon/manifest2.json"><link rel="mask-icon" href="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/favicon/cf_image2vector.svg" color="#5bbad5"><link rel="shortcut icon" href="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/favicon/32favicon32.ico"><link rel="stylesheet" href="/css/first.css"><link rel="stylesheet" href="/css/style.css" media="print" onload='this.media="all",this.onload=null'><noscript><link rel="stylesheet" href="/css/style.css"></noscript><script id="loadcss"></script><script>window.MSInputMethodContext&&document.documentMode&&document.write('<style>html{overflow-x: hidden !important;overflow-y: hidden !important;}.kill-ie{text-align:center;height: 100%;margin-top: 15%;margin-bottom: 5500%;}</style><div class="kill-ie"><h1><b>抱歉，您的浏览器无法访问本站</b></h1><h3>微软已经于2016年终止了对 Internet Explorer (IE) 10 及更早版本的支持，<br/>继续使用存在极大的安全隐患，请使用当代主流的浏览器进行访问。</h3><br/><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.microsoft.com/zh-cn/WindowsForBusiness/End-of-IE-support"><strong>了解详情 ></strong></a></div>')</script><noscript><style>html{overflow-x:hidden!important;overflow-y:hidden!important}.kill-noscript{text-align:center;height:100%;margin-top:15%;margin-bottom:5500%}</style><div class="kill-noscript"><h1><b>抱歉，您的浏览器无法访问本站</b></h1><h3>本页面需要浏览器支持（启用）JavaScript</h3><br> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.baidu.com/s?wd=启用JavaScript"><strong>了解详情 ></strong></a></div></noscript></head><body><header id="l_header" class="l_header always shadow blur show"><div class="container"><div id="wrapper"><div class="nav-sub"><p class="title"></p><ul class="switcher nav-list-h m-phone" id="pjax-header-nav-list"><li><a id="s-comment" class="fas fa-comments fa-fw" target="_self" href="javascript:void(0)" rel="external nofollow noreferrer"></a></li><li><a id="s-toc" class="s-toc fas fa-list fa-fw" target="_self" href="javascript:void(0)" rel="external nofollow noreferrer"></a></li></ul></div><div class="nav-main"> <a class="title flat-box" target="_self" href="/"><img no-lazy class="logo" src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/cover/fav/cf_o1x64.jpg"></a><div class="menu navigation"><ul class="nav-list-h m-pc"><li><a class="menuitem flat-box faa-parent animated-hover" href="/" id="home"><i class="fas fa-home fa-fw"></i> Home</a></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-th-list fa-fw"></i> Articles</a><ul class="list-v"><li><a class="menuitem flat-box faa-parent animated-hover" href="/categories/" id="categories"><i class="fas fa-folder-open fa-fw"></i> Categories</a></li><li><a class="menuitem flat-box faa-parent animated-hover" href="/tags/" id="tags"><i class="fas fa-tags fa-fw"></i> Tags</a></li><li><a class="menuitem flat-box faa-parent animated-hover" href="/archives/" id="archives"><i class="fas fa-archive fa-fw"></i> Archives</a></li></ul></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-fan faa-spin animated fa-fw"></i> Play</a><ul class="list-v"><li><a class="menuitem flat-box faa-parent animated-hover" href="/catchTheCat/" id="catchTheCat"><i class="fas fa-cat faa-wrench animated-hover fa-fw"></i> Chat Noir</a></li></ul></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-cog fa-spin fa-fw"></i> Tools</a><ul class="list-v"><li><a class="menuitem flat-box header toggle-mode-btn"><i class="fas fa-moon fa-fw"></i> 暗黑模式</a></li><li></li><li><a class="menuitem flat-box"><i class="fas fa-compact-disc fa-fw music"></i> Music</a><ul class="list-v"><li><div class="aplayer-container"><meting-js theme="#1BCDFC" autoplay="false" volume="0.7" loop="all" order="list" fixed="false" list-max-height="320px" server="netease" type="playlist" id="5472211102" list-folded="true"></meting-js></div></li></ul></li><li></li></ul></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-info-circle fa-fw"></i> Me</a><ul class="list-v"><li><a class="menuitem flat-box faa-parent animated-hover" href="/about/" id="about"><i class="fas fa-ellipsis-v fa-fw"></i> About</a></li><li><a class="menuitem flat-box faa-parent animated-hover" href="https://aner1001.gitee.io/resume/" target="_blank" id="https:aner1001giteeioresume"><i class="fas fa-clock fa-fw"></i> Resume</a></li></ul></li></ul></div><div id="tp-weather-widget"></div><script>!function(e,t,n,a,i,o,c,r){r=function(){o=t.createElement(n),c=t.getElementsByTagName(n)[0],o.src=i,o.charset="utf-8",o.async=1,c.parentNode.insertBefore(o,c)},e.SeniverseWeatherWidgetObject=a,e[a]||(e[a]=function(){(e[a].q=e[a].q||[]).push(arguments)}),e[a].l=+new Date,e.attachEvent?e.attachEvent("onload",r):e.addEventListener("load",r,!1)}(window,document,"script","SeniverseWeatherWidget","//cdn.sencdn.com/widget2/static/js/bundle.js?t="+parseInt(((new Date).getTime()/1e8).toString(),10)),window.SeniverseWeatherWidget("show",{flavor:"slim",location:"WTW3SJ5ZBJUY",geolocation:!0,language:"zh-Hans",unit:"c",theme:"auto",token:"563a4097-8461-469b-ac30-123bac0010d1",hover:"enabled",container:"tp-weather-widget"})</script><div class="m_search"><form name="searchform" class="form u-search-form"><i class="icon fas fa-search fa-fw"></i> <input type="text" class="input u-search-input" placeholder="Search..."></form></div><ul class="switcher nav-list-h m-phone"><li><a class="s-search fas fa-search fa-fw" target="_self" href="javascript:void(0)" rel="external nofollow noreferrer"></a></li><li><a class="s-menu fas fa-bars fa-fw" target="_self" href="javascript:void(0)" rel="external nofollow noreferrer"></a><ul class="menu-phone list-v navigation white-box"><li><a class="menuitem flat-box faa-parent animated-hover" href="/" id="home"><i class="fas fa-home fa-fw"></i> Home</a></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-th-list fa-fw"></i> Articles</a><ul class="list-v"><li><a class="menuitem flat-box faa-parent animated-hover" href="/categories/" id="categories"><i class="fas fa-folder-open fa-fw"></i> Categories</a></li><li><a class="menuitem flat-box faa-parent animated-hover" href="/tags/" id="tags"><i class="fas fa-tags fa-fw"></i> Tags</a></li><li><a class="menuitem flat-box faa-parent animated-hover" href="/archives/" id="archives"><i class="fas fa-archive fa-fw"></i> Archives</a></li></ul></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-fan faa-spin animated fa-fw"></i> Play</a><ul class="list-v"><li><a class="menuitem flat-box faa-parent animated-hover" href="/catchTheCat/" id="catchTheCat"><i class="fas fa-cat faa-wrench animated-hover fa-fw"></i> Chat Noir</a></li></ul></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-cog fa-spin fa-fw"></i> Tools</a><ul class="list-v"><li><a class="menuitem flat-box header toggle-mode-btn"><i class="fas fa-moon fa-fw"></i> 暗黑模式</a></li><li></li></ul></li><li><a class="menuitem flat-box faa-parent animated-hover"><i class="fas fa-info-circle fa-fw"></i> Me</a><ul class="list-v"><li><a class="menuitem flat-box faa-parent animated-hover" href="/about/" id="about"><i class="fas fa-ellipsis-v fa-fw"></i> About</a></li><li><a class="menuitem flat-box faa-parent animated-hover" href="https://aner1001.gitee.io/resume/" target="_blank" id="https:aner1001giteeioresume"><i class="fas fa-clock fa-fw"></i> Resume</a></li></ul></li></ul></li></ul></div></div></div></header><div id="l_body"><div id="l_cover"><div id="full" class="cover-wrapper post blank" style="display:none"><div id="cover-backstretch"></div><div id="scroll-down" style="display:none"><i class="fa fa-chevron-down scroll-down-effects"></i></div></div></div><div id="safearea"><div class="body-wrapper" id="pjax-container"><div class="l_main"><article class="article post white-box reveal md shadow article-type-post" id="post" itemscope itemprop="blogPost"><div class="article-meta" id="top"><h1 class="title"> Nginx学习笔记</h1><div class="new-meta-box"><div class="new-meta-item author"> <a class="author" href="/" rel="nofollow"><img no-lazy src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/cover/fav/cf_1o_x16.jpg"><p>汤晔飞</p></a></div><div class="new-meta-item category"><a class="notlink"><i class="fas fa-folder-open fa-fw" aria-hidden="true"></i> <a class="category-link" href="/categories/%E6%8A%80%E6%9C%AF%E7%9F%A5%E8%AF%86/">技术知识</a><span class="sep"></span><a class="category-link" href="/categories/%E6%8A%80%E6%9C%AF%E7%9F%A5%E8%AF%86/%E4%B8%AD%E9%97%B4%E4%BB%B6/">中间件</a></a></div><div class="new-meta-item date"><a class="notlink"><i class="fas fa-calendar-alt fa-fw" aria-hidden="true"></i><p>发布于：2021年5月13日</p></a></div><div class="new-meta-item browse leancloud"><a class="notlink"><div id="lc-pv" data-title="Nginx学习笔记" data-path="/posts/2021/05/13/fa918a90eed5/"><i class="fas fa-eye fa-fw" aria-hidden="true"></i><span id="number"><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span> 次浏览</div></a></div></div></div><p>Nginx主要用于1. 静态资源服务器，2. 反向代理，3. 负载均衡，本文主要围绕这三块进行描述。</p><span id="more"></span><h2 id="1-Nginx功能介绍"><a class="header-anchor" href="#1-Nginx功能介绍">☀</a>1.Nginx功能介绍</h2><ol><li><p>Nginx 是一款高性能的 http 服务器/反向代理服务器及电子邮件（IMAP/POP3）代理服务器。由俄罗斯的程序设计师伊戈尔·西索夫（Igor Sysoev）所开发，官方测试 nginx 能够支支撑 5 万并发链接，并且 cpu、内存等资源消耗却非常低，运行非常稳定。<br> <img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2021/03/08/20210309163501.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2021/03/08/20210309163501.png" srcset="" alt="image-20210309163501276"></p><p><strong>Nginx 应用场景：</strong></p><p>1、http 服务器。Nginx 是一个 http 服务可以独立提供 http 服务。可以做网页静态服务器。</p><p>2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。</p><p>3、反向代理，负载均衡。当网站的访问量达到一定程度后，单台服务器不能满足用户的请求时，需要用多台服务器集群可以使用 nginx 做反向代理。并且多台服务器可以平均分担负载，不会因为某台服务器负载高宕机而某台服务器闲置的情况。</p></li></ol><h2 id="2-下载和安装"><a class="header-anchor" href="#2-下载和安装">☀</a>2.下载和安装</h2><h3 id="下载"><a class="header-anchor" href="#下载">☀</a>下载</h3><p><a target="_blank" rel="noopener external nofollow noreferrer" href="http://nginx.org/en/download.html">Nginx下载地址</a></p><p><strong>学习所用相关工具：</strong></p><p>修改主机的Host文件：<a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/oldj/SwitchHosts/releases">SwitchHosts</a></p><h3 id="安装"><a class="header-anchor" href="#安装">☀</a>安装</h3><p>主要介绍nginx系统中的安装，windows系统，开箱即用。</p><h4 id="环境准备"><a class="header-anchor" href="#环境准备">☀</a>环境准备:</h4><p>（1）需要安装 gcc 的环境【此步省略】</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install gcc-c++</span><br></pre></td></tr></table></figure><p>（2）第三方的开发包。</p><blockquote><p><strong>PCRE</strong></p><p>PCRE(Perl Compatible Regular Expressions)是一个 Perl 库，包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式，所以需要在 linux 上安装 pcre 库。</p><p>注：pcre-devel 是使用 pcre 开发的一个二次开发库。nginx 也需要此库。</p></blockquote><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y pcre pcre-devel</span><br></pre></td></tr></table></figure><blockquote><p><strong>zlib</strong><br> zlib 库提供了很多种压缩和解压缩的方式，nginx 使用 zlib 对 http 包的内容进行 gzip，所以需要在 linux 上安装 zlib 库。</p></blockquote><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y zlib zlib-devel</span><br></pre></td></tr></table></figure><blockquote><p><strong>OpenSSL</strong><br> OpenSSL 是一个强大的安全套接字层密码库，囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议，并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议，还支持 https（即在 ssl 协议上传输 http），所以需要在 linux安装 openssl 库。</p></blockquote><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y openssl openssl-devel</span><br></pre></td></tr></table></figure><h4 id="步骤"><a class="header-anchor" href="#步骤">☀</a>步骤:</h4><p><strong>第一步：上传</strong><br> 把 nginx 的源码包nginx-1.8.0.tar.gz上传到 linux 系统</p><p><strong>第二步：解压缩</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar zxvf nginx-1.8.0.tar.gz  </span><br></pre></td></tr></table></figure><p><strong>第三步：创建配置文件</strong><br> 进入nginx-1.8.0目录 使用 configure 命令创建一 makeFile 文件。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">./configure \</span><br><span class="line">--prefix=/usr/local/nginx \</span><br><span class="line">--pid-path=/var/run/nginx/nginx.pid \</span><br><span class="line">--lock-path=/var/lock/nginx.lock \</span><br><span class="line">--error-log-path=/var/log/nginx/error.log \</span><br><span class="line">--http-log-path=/var/log/nginx/access.log \</span><br><span class="line">--with-http_gzip_static_module \</span><br><span class="line">--http-client-body-temp-path=/var/temp/nginx/client \</span><br><span class="line">--http-proxy-temp-path=/var/temp/nginx/proxy \</span><br><span class="line">--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \</span><br><span class="line">--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \</span><br><span class="line">--http-scgi-temp-path=/var/temp/nginx/scgi</span><br></pre></td></tr></table></figure><p>执行后可以看到Makefile文件<br> <img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2021/03/08/20210309165245.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2021/03/08/20210309165245.png" srcset="" alt="image-20210309165245552"></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">----  知识点小贴士 ----</span><br><span class="line">Makefile是一种配置文件， Makefile 一个工程中的源文件不计数，其按类型、功能、模块分别放在若干个目录中，makefile定义了一系列的规则来指定，哪些文件需要先编译，哪些文件需要后编译，哪些文件需要重新编译，甚至于进行更复杂的功能操作，因为 makefile就像一个Shell脚本一样，其中也可以执行操作系统的命令。</span><br><span class="line">---  知识点小贴士 ----</span><br><span class="line">configure参数</span><br><span class="line">./configure \</span><br><span class="line">--prefix=/usr \                                                        指向安装目录</span><br><span class="line">--sbin-path=/usr/sbin/nginx \                                 指向（执行）程序文件（nginx）</span><br><span class="line">--conf-path=/etc/nginx/nginx.conf \                      指向配置文件</span><br><span class="line">--error-log-path=/var/log/nginx/error.log \              指向<span class="built_in">log</span></span><br><span class="line">--http-log-path=/var/log/nginx/access.log \            指向http-log</span><br><span class="line">--pid-path=/var/run/nginx/nginx.pid \                      指向pid</span><br><span class="line">--lock-path=/var/lock/nginx.lock \                         （安装文件锁定，防止安装文件被别人利用，或自己误操作。）</span><br><span class="line">--user=nginx \</span><br><span class="line">--group=nginx \</span><br><span class="line">--with-http_ssl_module \                      启用ngx_http_ssl_module支持（使支持https请求，需已安装openssl）</span><br><span class="line">--with-http_flv_module \                       启用ngx_http_flv_module支持（提供寻求内存使用基于时间的偏移量文件）</span><br><span class="line">--with-http_stub_status_module \     启用ngx_http_stub_status_module支持（获取nginx自上次启动以来的工作状态）</span><br><span class="line">--with-http_gzip_static_module \   启用ngx_http_gzip_static_module支持（在线实时压缩输出数据流）</span><br><span class="line">--http-client-body-temp-path=/var/tmp/nginx/client/ \ 设定http客户端请求临时文件路径</span><br><span class="line">--http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 设定http代理临时文件路径</span><br><span class="line">--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 设定http fastcgi临时文件路径</span><br><span class="line">--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 设定http uwsgi临时文件路径</span><br><span class="line">--http-scgi-temp-path=/var/tmp/nginx/scgi \ 设定http scgi临时文件路径</span><br><span class="line">--with-pcre 启用pcre库</span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong>第四步：编译</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make</span><br></pre></td></tr></table></figure><p><strong>第五步：安装</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make install</span><br></pre></td></tr></table></figure><p><strong>第六步：启动</strong></p><p>注意：启动nginx 之前，上边将临时文件目录指定为/var/temp/nginx/client， 需要在/var 下创建此 目录</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mkdir</span> /var/temp/nginx/client -p</span><br></pre></td></tr></table></figure><p>进入到Nginx目录下的sbin目录</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /usr/local/ngiux/sbin</span><br></pre></td></tr></table></figure><p>输入命令启动Nginx</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./nginx</span><br></pre></td></tr></table></figure><p>启动后查看进程</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps aux|grep nginx</span><br></pre></td></tr></table></figure><p>地址栏输入虚拟机的IP即可访问（默认为80端口）</p><p>关闭 nginx：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./nginx -s stop</span><br></pre></td></tr></table></figure><p>或者</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./nginx -s quit</span><br></pre></td></tr></table></figure><p>重启 nginx：<br> 1、先关闭后启动。<br> 2、执行重启指令,刷新配置文件：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./nginx -s reload</span><br></pre></td></tr></table></figure><h2 id="3-配置介绍"><a class="header-anchor" href="#3-配置介绍">☀</a>3.配置介绍</h2><p><strong>nginx.conf</strong></p><p>配置文件所在路径：在nginx安装目录的conf目录下。</p><h3 id="server"><a class="header-anchor" href="#server">☀</a>server</h3><p><code>server&#123;&#125;</code></p><p>表示代理的某个服务，下方的配置都在这个server括号中。</p><h3 id="listen"><a class="header-anchor" href="#listen">☀</a>listen</h3><p><code>listen 80</code></p><p>表示服务所监听的端口号</p><h3 id="server-name"><a class="header-anchor" href="#server-name">☀</a>server_name</h3><p><code>server_name</code></p><p><strong>有匹配规则</strong>，与浏览器上输入的域名匹配。优先匹配最精确的。</p><p>举例：</p><ol><li>==localhost== 指向本机</li><li>==www.tangyefei. com== 域名，最终指向本机</li></ol><h3 id="location"><a class="header-anchor" href="#location">☀</a>location</h3><p><code>location</code></p><p><strong>匹配规则</strong>，在域名ip和端口号后的地址匹配，/表示全部匹配。</p><p>==注意：==路径的间隔都使用<code>/</code>。</p><p><strong>location</strong>匹配模式及顺序</p><table><thead><tr><th>表示</th><th>描述</th></tr></thead><tbody><tr><td>location = /uri地址</td><td>=开头表示精确匹配，只有完全匹配上才能生效。</td></tr><tr><td>location ^~ /uri地址</td><td>^~ 开头对URL路径进行前缀匹配，并且在正则之前。</td></tr><tr><td>location ~ 正则表达式</td><td>~开头表示区分大小写的正则匹配。</td></tr><tr><td>location ~* 正则表达式</td><td>~*开头表示不区分大小写的正则匹配。</td></tr><tr><td>location /uri地址</td><td>不带任何修饰符，也表示前缀匹配，但是在正则匹配之后。</td></tr><tr><td>location /</td><td>通用匹配，拦截所有，但是优先级最低，只有前面都没有被拦截的情况下，才会被拦截到这里。</td></tr></tbody></table><h4 id="root"><a class="header-anchor" href="#root">☀</a>root</h4><p><code>root</code> 在location中配置</p><p>表示匹配成功后进入主机的目录</p><p><strong>相对路径以niginx安装位置为基础</strong>，通常是绝对路径。</p><p>==注意：路径都使用<code>/</code>来间隔文件夹。root 路径最后不能为<code>/</code>结束。==</p><p>匹配成功查找资源时，root+地址中的location。</p><h4 id="index"><a class="header-anchor" href="#index">☀</a>index</h4><p><code>index</code> 在location中配置 在location中配置</p><p>默认打开的页面 <strong>如果地址匹配成功，并且进入的是某个目录，而不是具体文件，则打开root+location地址中的index指定的文件</strong>。</p><h4 id="proxy-pass"><a class="header-anchor" href="#proxy-pass">☀</a><strong>proxy_pass</strong></h4><p><code>proxy_pass</code>反向代理配置 在location中配置</p><blockquote><p><strong>proxy_pass:</strong> 127.0.0.1可以换成任何一个通的内网地址，这个ip表示你要真实访问的tomcat所在的位置，proxy_pass的值就表示你真正访问的域名是什么（站在公网服务器角度来说）。</p></blockquote><p>真正要访问并且代理的服务器地址。</p><p>可配置方式：</p><p>ip+端口或者域名(有默认端口或者映射的端口)</p><p>ip+端口+某个路径</p><p>一般配置了proxy_pass就不需要配置root了。root指向本机资源。proxy_pass指向代理服务器。</p><p><strong>最终访问的方式</strong>：</p><p>proxy_pass+location</p><h4 id="proxy-redirect-off"><a class="header-anchor" href="#proxy-redirect-off">☀</a><em>proxy_redirect off</em></h4><p>在location中配置</p><h4 id="proxy-set-header"><a class="header-anchor" href="#proxy-set-header">☀</a><em>proxy_set_header</em></h4><p>在location中配置</p><p><code>proxy_set_header Host $host</code></p><p><code>proxy_set_header X-Real-IP $remote_addr</code></p><p><code>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for</code></p><h4 id="proxy-connect-timeout"><a class="header-anchor" href="#proxy-connect-timeout">☀</a><strong>proxy_connect_timeout</strong></h4><blockquote><p>nginx向tomcat发起连接，即第一次握手等待tomcat回应的超时时间，tomcat的这次回应只是说明能正常连接，并没有响应具体请求的内容。</p></blockquote><p>理解为确认连接的时间。</p><p><code>proxy_connect_timeout 3s;</code></p><h4 id="proxy-send-timeout"><a class="header-anchor" href="#proxy-send-timeout">☀</a><strong>proxy_send_timeout</strong></h4><blockquote><p>nginx将请求发送给tomcat的超时时间，应该是确认能正常连接之后向tomcat发送真正的业务请求。</p></blockquote><p>理解为建立了连接口，发送请求的时间。</p><p><code>proxy_send_timeout 3s;</code></p><h4 id="proxy-read-timeout"><a class="header-anchor" href="#proxy-read-timeout">☀</a><strong>proxy_read_timeout</strong></h4><blockquote><p>tomcat接受到真正业务请求之后，nginx等待tomcat响应具体请求的内容的超时时间。差不多可以理解tomcat处理具体请求时间的最大值，也就是tomcat必须在这个时间内做完业务逻辑处理。</p></blockquote><p>理解为发送请求成功后，处理和响应的时间。</p><p><code>proxy_read_timeout 5s;</code></p><h4 id="proxy-cookie-path"><a class="header-anchor" href="#proxy-cookie-path">☀</a>proxy_cookie_path</h4><p>作用：将服务器返回的 <code>Set-Cookie</code> 中的path进行修改。</p><p>应用场景:转载自：<a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.jb51.net/article/187898.htm">https://www.jb51.net/article/187898.htm</a></p><p><strong>背景</strong></p><p>一同事求援：后台系统的登录成功了，但不能成功登进系统，仍然跳转到登录页，但同一套代码另一个环境却没有问题。</p><p>经了解，他对同一个项目使用tomcat部署了两个环境，一个在开发服务器上，一个在他本机，两个环境代码配置完全相同。两边通过同一个nginx进行反向代理，nginx配置大致如下，</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">location /health/ &#123;</span><br><span class="line">  proxy_pass http://192.168.40.159:8081/health/; #无问题的配置</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line">location /health-dev/ &#123;</span><br><span class="line">  proxy_pass http://192.168.40.202:8080/health/; #有问题的配置</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>一个反向代理到开发环境，一个反向代理到本机服务。</p><p><strong>定位</strong></p><p>既然代码配置完全相同，那么问题很大可能就出现在nginx的反向代理上。</p><p>因为两边location路径不同（即浏览器路径不同），但是反向代理的服务端路径却相同，结合session的基本原理，如下图，</p><p><img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/03/20201203192943.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/03/20201203192943.png" srcset="" alt="img"></p><ol><li>当浏览器第一次打开页面时，服务端会为这次会话创建一个session，并将session id通过response的header传递给浏览器，header一般为 Set-Cookie: JSESSIONID=xxxxx; Path=xxxx</li><li>浏览器接收到响应后，如果header Set-Cookie 中path的值与浏览器地址路径匹配，则将该header值存于浏览器的Cookie中</li><li>浏览器在下次请求服务器时，将Cookie中的JSESSIONID值通过request的header上报给服务端，header一般为 Cookie: JSESSIONID=xxxx;</li><li>服务端可通过该JSESSIONID来定位到对应的session</li></ol><p>nginx反向代理按这种方式配置时</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location /health-dev/ &#123;</span><br><span class="line">  proxy_pass http://192.168.40.202:8080/health/;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>浏览器访问 <code>http://www.domian.com/health-dev</code> 时，服务端返回的 <code>Set-Cookie</code> 的 Path 值为 <code>/health</code> （因为中间有反向代理，服务端并不知道代理前的路径是啥，是按最终请求服务端的路径设置），如图</p><p><img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/03/20201203192952.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/03/20201203192952.png" srcset="" alt="img"></p><p>因为浏览器访问地址的路径 <code>/health-dev</code> 与 <code>Set-Cookie</code> 的 Path <code>/health</code> 不匹配，所以浏览器并不会将其值存入Cookie中，如图</p><p><img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/03/20201203193000.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/03/20201203193000.png" srcset="" alt="img"></p><p>因此在下次请求服务器时，浏览器无法设置request <code>Cookie</code> header的 <code>JSESSIONID</code> 值，服务器无法定位到对应的session，因此会将其当做第一次请求，创建一个新的session，如此反复，因此就算你登录认证通过了，但服务器返回的登录凭证（JSESSIONID）浏览器不会保存，并在下次请求时携带，导致服务器认为你是一个新的请求，当然就会又跳到登录页面了。</p><p><strong>解决</strong></p><p>nginx有一个命令 <code>proxy_cookie_path</code> （参考： <a target="_blank" rel="noopener external nofollow noreferrer" href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cookie_path">proxy_cookie_path</a> ）可将服务器返回的 <code>Set-Cookie</code> 中的path进行修改，格式为 <code>proxy_cookie_path 原路径 目标路径</code> ，我们在配置中添加 <code>proxy_cookie_path</code> 如下。</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">location</span> /health-dev/ &#123;</span><br><span class="line">  <span class="attribute">proxy_pass</span> http://192.168.40.202:8080/health/;</span><br><span class="line">  <span class="attribute">proxy_cookie_path</span> /health /health-dev;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>重启nginx，问题解决。</p><h4 id="proxy-cookie-domain"><a class="header-anchor" href="#proxy-cookie-domain">☀</a>proxy_cookie_domain</h4><p>作用：修改response中的，set-cookie的domain属性，可以控制cookie的生效域名目标</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">location</span> /api &#123;</span><br><span class="line">   <span class="attribute">proxy_pass</span> https://b.test.com;</span><br><span class="line">   <span class="attribute">proxy_cookie_domain</span> b.test.com  a.test.com;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><blockquote><p>proxy_cookie_domain的作用是单向的，并不是双向转换的。<br> <img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/tangtang/20210203201456.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/tangtang/20210203201456.png" srcset="" alt="img"></p><p>浏览器在发送请求的时候，会在request header中带上cookie项(有内容的话)，此时的cookie是一个字符串，一个key=value并用分号分割的字符串，<br> <img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/06/20201206160039.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/06/20201206160039.png" srcset="" alt="img"></p><p>其中<strong>并不包含任何域名信息</strong>。这是因为浏览器在设置cookie选项的时候，所选取的内容都是缓存中接口域名下的。然后request的只要请求发送出去之后，cookie中有关domain信息其实是不存在的，它只是一个普通的字符串，随便proxy_pass到任何位置，都会正常携带下去。因此<strong>在前端到后端的request的过程中，proxy_cookie_domain是没用的</strong></p><p>而server端在做响应的时候，通过set-cookie的domain属性，可以控制cookie的生效域名目标，做到诸如二级域名cookie分离等等，如果前端接收到的set-cookie的domain和当前域名不一致，或者一级域名不一致(二级域名可以共享一级域名下的cookie)，这个cookie在后续的通信中就是无效的，所以这里才需要去做domain的转换，也就是说<strong>response中set-cookie的domain转换才是有意义的</strong>，这也正是proxy_cookie_domain的作用所在。<br> 当response的set-cookie中domain不去设置时，cookie顺利传入浏览器中，浏览器会自动设置这个cookie的生效域名为当前域名。</p></blockquote><h4 id="add-header"><a class="header-anchor" href="#add-header">☀</a>add_header</h4><p>配置在location中。</p><blockquote><p>add_header 是 headers 模块中定义的一个指令，顾名思义就是用来添加 http 响应头的。但请注意他只是「添加」而已，并不是重写。所以如果已经存在某个头，再使用 add_header 就会出问题。而且在低版本的 nginx 中 add_header 还不支持在错误页面中使用。</p></blockquote><h4 id="rewrite-TODO"><a class="header-anchor" href="#rewrite-TODO">☀</a>rewrite //TODO</h4><h4 id="return"><a class="header-anchor" href="#return">☀</a>return</h4><p>配置在location中</p><p>设置响应的状态信息</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">location</span> = /b &#123;</span><br><span class="line"> <span class="attribute">add_header</span> b <span class="number">2</span>;</span><br><span class="line"> <span class="attribute">return</span> <span class="number">204</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="section">location</span> = /a &#123;</span><br><span class="line"> <span class="attribute">add_header</span> Content-Type application/json;</span><br><span class="line"> <span class="attribute">return</span> <span class="number">200</span> <span class="string">&#x27;&quot;OK&quot;&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="section">location</span> = /a &#123;</span><br><span class="line"> <span class="attribute">add_header</span> Content-Type application/json;</span><br><span class="line"> <span class="attribute">return</span> <span class="number">404</span> <span class="string">&#x27;&quot;Not Found&quot;&#x27;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/06/20201206183940.png" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/blog/2020/12/06/20201206183940.png" srcset="" alt="img"></p><h3 id="default-type"><a class="header-anchor" href="#default-type">☀</a>default_type</h3><p>配置在server中。</p><p>全局设置response响应的Content-Type参数</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">default_type</span> <span class="string">&#x27;text/plain&#x27;</span>;</span><br><span class="line"><span class="section">location</span> = /a &#123;</span><br><span class="line"> <span class="comment">#这里和全局设置冲突。会重复。</span></span><br><span class="line"> <span class="attribute">add_header</span> Content-Type application/json;</span><br><span class="line"> <span class="attribute">return</span> <span class="number">200</span> <span class="string">&#x27;&quot;OK&quot;&#x27;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="upstram-XXX"><a class="header-anchor" href="#upstram-XXX">☀</a><strong>upstram XXX{}</strong></h3><p>与server{}同级配置</p><p>表示负载均衡的配置，XXX</p><p>轮巡算法默认<br> 权重算法在server中添加 weight = X<br> ip绑定算法，添加ip_hash</p><h4 id="server-2"><a class="header-anchor" href="#server-2">☀</a>server</h4><p>​ 负载均衡服务器，上游的服务器，通常格式是<code>Ip:Port</code></p><h4 id="ip-hash"><a class="header-anchor" href="#ip-hash">☀</a>ip_hash</h4><p>​ 表示使用负载均衡的ip绑定算法。</p><h2 id="4-反向代理"><a class="header-anchor" href="#4-反向代理">☀</a>4.反向代理</h2><p>==域名可以解析为IP+端口==，猜测如果指向本机，默认端口为80。</p><blockquote><p>**实质：**个人理解反向代理就是nginx拦截动态请求之后转发给某个tomcat。这个在==集群==和==分布式==都可以使用这个来进行配置转发。</p></blockquote><blockquote><p><strong>作用：</strong> 隐藏真实的访问ip地址。我们可以看到流程图中我们访问的最多也就是公网的ip，但是具体tomcat在那个ip是不知道的，这样就能减少tomcat被攻击，提高了服务器的安全性。</p></blockquote><p><img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/tangtang/20210203201442" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/tangtang/20210203201442" srcset="" alt="这里写图片描述"></p><h2 id="5-动静分离"><a class="header-anchor" href="#5-动静分离">☀</a>5.动静分离</h2><h3 id="概念"><a class="header-anchor" href="#概念">☀</a>概念</h3><h4 id="静态资源："><a class="header-anchor" href="#静态资源：">☀</a>静态资源：</h4><p>静态资源： html、js、css、图片、音乐、视频等。当用户多次访问这个资源，资源的源代码永远不会改变或者很少改变的资源。</p><h4 id="动态资源："><a class="header-anchor" href="#动态资源：">☀</a>动态资源：</h4><p>动态资源：当用户多次访问这个资源，资源的源代码可能会发生改变。</p><p>​ &gt; 我的理解就是我们所说的接口。这里需要注意的是：themleaf、freemark这些模板引擎的html不是静态资源而应该属于动态资源（个人认为）。</p><h4 id="动静分离："><a class="header-anchor" href="#动静分离：">☀</a>动静分离：</h4><p>动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来，简单的概括是：动态文件与静态文件的分离。动静资源做好了拆分以后，我们就可以根据静态资源的特点将其做缓存操作，这就是网站静态化处理的核心思路</p><h4 id="伪静态"><a class="header-anchor" href="#伪静态">☀</a>伪静态</h4><p>伪静态：网站如果想被搜索引擎搜素到，动态页面静态技术freemarker等模版引擎技术</p><h3 id="动静分离原因："><a class="header-anchor" href="#动静分离原因：">☀</a>动静分离原因：</h3><blockquote><ul><li>在我们的软件开发中，有些请求是需要后台处理的（如：.jsp,.do等等），有些请求是不需要经过后台处理的（如：css、html、jpg、js等等文件），这些不需要经过后台处理的文件称为静态文件，否则动态文件。因此我们后台处理忽略静态文件。这会有人又说那我后台忽略静态文件不就完了吗。==当然这是可以的，但是这样后台的请求次数就明显增多了。==在我们对资源的响应速度有要求的时候，我们应该使用这种动静分离的策略去解决。</li><li>动静分离将网站静态资源（HTML，JavaScript，CSS，img等文件）与后台应用分开部署，提高用户访问静态代码的速度，降低对后台应用访问。这里我们将静态资源放到nginx中，动态资源转发到tomcat服务器中。</li><li>因此，动态资源转发到tomcat服务器我们就使用到了前面讲到的反向代理了。</li></ul></blockquote><blockquote><p>之前项目中需要用到百度地图，由于项目的特殊环境，所以需要下载百度的离线地图。我们知道，地图的展示其实就是一张张地图的图片展示，我们在网页上浏览查找位置的时候，其实都是在发送图片请求。这些图片请求相对于jsp，servlet来说就是所谓的静态资源，当然服务中的静态资源不仅仅只是图片，像页面样式css文件，js脚本文件这些都可以看着是静态资源。==tomcat既可以静态资源也可以处理动态资源。但是单节点的tomcat本身处理请求的资源是有限的，如果项目中的tomcat既处理动态请求有要处理大量的静态资源。显然是不合理的，也就是会遇到tomcat的性能瓶颈问题，对于静态资源的处理我们有更好的服务器像Nginx,Apache.他们处理静态资源的能力比Tomcat强很多==，那么我们能不能对请求进行分工呢?静态资源的请求由Nginx来处理，像jsp我们交给tomcat来处理。这样也能减轻tomcat的处理压力。</p></blockquote><p><strong>==个人理解：==</strong></p><p>Tomcat也可以处理静态资源，动静分离也可以分两个Tomcat减缓访问压力。Nginx处理静态资源能力更加优秀。</p><p>总结：</p><p>1.提高用户访问静态代码的速度，降低对后台应用访问，降低后台压力。</p><p>2.总体提高资源的响应速度。</p><h3 id="实现动静分离的两种方法："><a class="header-anchor" href="#实现动静分离的两种方法：">☀</a><strong>实现动静分离的两种方法：</strong></h3><h4 id="通过不同域名来拦截"><a class="header-anchor" href="#通过不同域名来拦截">☀</a>通过不同域名来拦截</h4><blockquote><p>动态请求和静态请求使用不同的域名。比如所有的静态资源都使用static.tuesdayma.com域名来访问，所有的接口都使用www.tuesdayma.com来请求。</p></blockquote><p>图片理解：</p><p><img src="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/tangtang/20210203201442" class="lazyload" data-srcset="https://yefeit-note-picbed.oss-cn-shanghai.aliyuncs.com/img/tangtang/20210203201442" srcset="" alt="img"></p><p>==优点：==扩展性比较强，静态资源是什么都可以。</p><p>==缺点：==存在跨域问题，所有的html全靠ajax请求来请求接口，后端域名和前端不一致导致跨域问题,<strong>实际是因为配置了两个server导致跨域</strong>。</p><h4 id="location来拦截"><a class="header-anchor" href="#location来拦截">☀</a>location来拦截</h4><p>只配置一个server，然后配置两个location，一个通过正则表达式拦截静态资源，还有一个拦截.do结尾的接口请求。</p><p>==优点：==不存在跨域问题。</p><p>==缺点：==扩展性太差，静态资源的类型不可确定，每增加一种类型都需要重新修改配置文件并且重启nginx。</p><h2 id="6-负载均衡"><a class="header-anchor" href="#6-负载均衡">☀</a>6.负载均衡</h2><h3 id="原因与作用："><a class="header-anchor" href="#原因与作用：">☀</a>原因与作用：</h3><blockquote><p>为了解决高并发问题，负载均衡服务器拦截所有的请求，采用负载均衡算法，分配到不同的tomcat上。</p></blockquote><blockquote><p><strong>作用：</strong> 减少单台tomcat的压力</p></blockquote><h3 id="三种基本的负载均衡算法："><a class="header-anchor" href="#三种基本的负载均衡算法：">☀</a><strong>三种基本的负载均衡算法：</strong></h3><ol><li><p>轮询</p><blockquote><p>nginx==默认==的负载均衡算法，简单来说就是从上到下按顺序轮流（127.0.0.1:8082轮完就轮到127.0.0.1:8081，127.0.0.1:8081轮完就轮到127.0.0.1:8082）。注意：mzd的地方需要保持一致。。。</p></blockquote><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">upstream  mzd &#123;</span><br><span class="line">      server 127.0.0.1:8082;</span><br><span class="line">      server 127.0.0.1:8081;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></li><li><p>权重</p><blockquote><p>我的理解就是哪台服务器配置好就多轮几次，或者你就想某个服务器多轮几次。简单来说就是轮到次数的比例，数字越大表示轮到的概率越大。（个人认为weight都设置为1的时候和轮询没什么区别）</p></blockquote><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">upstream  mzd &#123;</span><br><span class="line">      server 127.0.0.1:8082 weight=2;</span><br><span class="line">      server 127.0.0.1:8081 weight=3;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></li><li><p>ip绑定</p><blockquote><p>我的理解就是你第一次访问的时候，nginx会根据你的ip通过哈希算法，算出某个值，然后取分配哪个tomcat，当你第二次访问，第三次访问。。。之后的任何一次访问都是去请求那个第一次访问的tomcat。</p></blockquote><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">upstream  mzd &#123;</span><br><span class="line">      server 127.0.0.1:8082 ;</span><br><span class="line">      server 127.0.0.1:8081 ;</span><br><span class="line">      ip_hash;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></li></ol><h2 id="7-故障转移机制"><a class="header-anchor" href="#7-故障转移机制">☀</a>7.故障转移机制</h2><p>在负载均衡下存在故障转移机制</p><blockquote><p>其实nginx有默认故障转移机制的，但是很慢（本人测了一下，默认好像是要60秒左右，一分钟后就有自动发送到其他tomcat上去了</p></blockquote><p>可通过手工配置超时时间：</p><p>proxy_connect_timeout<br> proxy_send_timeout<br> proxy_read_timeout</p><h2 id="8-命令"><a class="header-anchor" href="#8-命令">☀</a>8.命令</h2><h3 id="Windows"><a class="header-anchor" href="#Windows">☀</a>Windows</h3><ol><li><p>启动<br> CMD切换到nginx.exe目录下：</p><p>start nginx</p><p>nginx.exe</p></li><li><p>停止<br> nginx.exe -s stop<br> nginx.exe -s quit</p><blockquote><p>stop是快速停止nginx，可能并不保存相关信息；quit是完整有序的停止nginx，并保存相关信息。</p></blockquote><p>==注意：==使用停止命令的时候，不能修改nginx.conf文件，在原启动时配置的文件基础上停止。否则可能会报d错。nginx: [error] CreateFile()</p></li><li><p>测试配置文件是否有效<br> nginx.exe -t</p></li><li><p>重新载入<br> nginx.exe -s reload</p><blockquote><p>当配置信息修改，需要重新载入这些配置时使用此命令。</p></blockquote></li><li><p>重新打开日志文件<br> nginx.exe -s reopen</p></li><li><p>查看nginx版本<br> nginx -v</p></li></ol><h3 id="Linux"><a class="header-anchor" href="#Linux">☀</a>Linux</h3><p>1.启动</p><p>cd 到nginx安装路径的sbin目录下</p><p>./nginx</p><p>2.测试配置文件是否有效</p><p>./nginx -t</p><p>3.重启nginx</p><p>./nginx -s reload</p><p>4.查看nginx版本</p><p>./nginx -v</p><h2 id="9-配置案例："><a class="header-anchor" href="#9-配置案例：">☀</a>9.配置案例：</h2><h3 id="1-静态资源服务器"><a class="header-anchor" href="#1-静态资源服务器">☀</a>1.静态资源服务器</h3><h4 id="root相对路径"><a class="header-anchor" href="#root相对路径">☀</a>root相对路径</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">      listen       80;</span><br><span class="line">      server_name  www.tuesdayma.com;</span><br><span class="line">      location / &#123;</span><br><span class="line">          root   html;</span><br><span class="line">          index  index.html index.htm;</span><br><span class="line">      &#125;</span><br><span class="line">      error_page   500 502 503 504  /50x.html;</span><br><span class="line">      location = /50x.html &#123;</span><br><span class="line">          root   html;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure><h4 id="域名匹配访问不同资源"><a class="header-anchor" href="#域名匹配访问不同资源">☀</a>域名匹配访问不同资源</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">     listen       80;</span><br><span class="line">     server_name  static.Tuesdayma.com;</span><br><span class="line">     location / &#123;</span><br><span class="line">         root   extend/static;</span><br><span class="line">         index  index.html index.htm;</span><br><span class="line">     &#125;</span><br><span class="line">     error_page   500 502 503 504  /50x.html;</span><br><span class="line">     location = /50x.html &#123;</span><br><span class="line">         root   html;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line">server &#123;</span><br><span class="line">     listen       80;</span><br><span class="line">     server_name  www.Tuesdayma.com;</span><br><span class="line">     location / &#123;</span><br><span class="line">         root   extend/www;</span><br><span class="line">         index  index.html index.htm;</span><br><span class="line">     &#125;</span><br><span class="line">     error_page   500 502 503 504  /50x.html;</span><br><span class="line">     location = /50x.html &#123;</span><br><span class="line">         root   html;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure><h3 id="2-反向代理"><a class="header-anchor" href="#2-反向代理">☀</a>2.反向代理</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> server &#123;</span><br><span class="line">    listen       80;</span><br><span class="line">    server_name  www.tuesdayma.com;</span><br><span class="line">    location / &#123;</span><br><span class="line">        proxy_pass   http://127.0.0.1:8090;</span><br><span class="line">        index  index.html index.htm;</span><br><span class="line">    &#125;</span><br><span class="line">    error_page   500 502 503 504  /50x.html;</span><br><span class="line">    location = /50x.html &#123;</span><br><span class="line">        root   html;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="3-动静分离"><a class="header-anchor" href="#3-动静分离">☀</a>3.动静分离</h3><h4 id="通过不同域名来拦截-2"><a class="header-anchor" href="#通过不同域名来拦截-2">☀</a>通过不同域名来拦截</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">#动态请求拦截</span><br><span class="line"> server &#123;</span><br><span class="line">       listen       80;</span><br><span class="line">       server_name  www.Tuesdayma.com;</span><br><span class="line">       location / &#123;</span><br><span class="line">          # proxy_redirect off;  </span><br><span class="line">          # proxy_set_header Host $host;  </span><br><span class="line">          # proxy_set_header X-Real-IP $remote_addr;  </span><br><span class="line">          # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  </span><br><span class="line">           proxy_pass   http://127.0.0.1:8080;</span><br><span class="line">       &#125;</span><br><span class="line">       error_page   500 502 503 504  /50x.html;</span><br><span class="line">       location = /50x.html &#123;</span><br><span class="line">           root   html;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">  #静态请求拦截</span><br><span class="line">  server &#123;</span><br><span class="line">       listen       80;</span><br><span class="line">       server_name  static.Tuesdayma.com;</span><br><span class="line">       location /nginx_static&#123;</span><br><span class="line">           root   F:/;</span><br><span class="line">           index  index.html index.htm;</span><br><span class="line">       &#125;</span><br><span class="line">       error_page   500 502 503 504  /50x.html;</span><br><span class="line">       location = /50x.html &#123;</span><br><span class="line">           root   html;</span><br><span class="line">       &#125;  </span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure><h3 id="4-负载均衡"><a class="header-anchor" href="#4-负载均衡">☀</a>4.负载均衡</h3><h4 id="1-轮询"><a class="header-anchor" href="#1-轮询">☀</a>1.<strong>轮询</strong></h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">upstream  mzd &#123;</span><br><span class="line">      server 127.0.0.1:8082;</span><br><span class="line">      server 127.0.0.1:8081;</span><br><span class="line">   &#125;</span><br><span class="line">   server &#123;</span><br><span class="line">       listen       80;</span><br><span class="line">       server_name  www.tuesdayma.com;</span><br><span class="line">       location / &#123;</span><br><span class="line">           proxy_pass  http://mzd;</span><br><span class="line">           index  index.html index.htm;</span><br><span class="line">       &#125;</span><br><span class="line">       error_page   500 502 503 504  /50x.html;</span><br><span class="line">       location = /50x.html &#123;</span><br><span class="line">           root   html;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure><p>本机nginx的负载：</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">upstream tw&#123;</span><br><span class="line">	server 127.0.0.1:8080;</span><br><span class="line">	server 127.0.0.1:8081;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">       listen       8080;</span><br><span class="line">       server_name  127.0.0.1;</span><br><span class="line">       location / &#123;</span><br><span class="line">           root  D:/Data/Html/nginx1;</span><br><span class="line">           index  nginx1.html;</span><br><span class="line">       &#125;</span><br><span class="line">       error_page   500 502 503 504  /50x.html;</span><br><span class="line">       location = /50x.html &#123;</span><br><span class="line">           root   html;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">       listen       8081;</span><br><span class="line">       server_name  127.0.0.1;</span><br><span class="line">       location / &#123;</span><br><span class="line">           root  D:/Data/Html/nginx2;</span><br><span class="line">           index  nginx2.html;</span><br><span class="line">       &#125;</span><br><span class="line">       error_page   500 502 503 504  /50x.html;</span><br><span class="line">       location = /50x.html &#123;</span><br><span class="line">           root   html;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   server &#123;</span><br><span class="line">       listen       80;</span><br><span class="line">       server_name  www.tangyefei.com;</span><br><span class="line"></span><br><span class="line">       location / &#123;</span><br><span class="line">		proxy_pass  http://tw;</span><br><span class="line">           index  xiaowan.html;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	location /Html &#123;</span><br><span class="line">           root   D:/Data;</span><br><span class="line">           index  index.html index.htm;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       error_page   500 502 503 504  /50x.html;</span><br><span class="line">       location = /50x.html &#123;</span><br><span class="line">           root   html;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><h4 id="2-权重"><a class="header-anchor" href="#2-权重">☀</a>2.<strong>权重</strong></h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">upstream  mzd &#123;</span><br><span class="line">      server 127.0.0.1:8082 weight=2;</span><br><span class="line">      server 127.0.0.1:8081 weight=3;</span><br><span class="line">   &#125;</span><br><span class="line">   server &#123;</span><br><span class="line">       listen       80;</span><br><span class="line">       server_name  www.tuesdayma.com;</span><br><span class="line">       location / &#123;</span><br><span class="line">           proxy_pass  http://mzd;</span><br><span class="line">           index  index.html index.htm;</span><br><span class="line">       &#125;</span><br><span class="line">       error_page   500 502 503 504  /50x.html;</span><br><span class="line">       location = /50x.html &#123;</span><br><span class="line">           root   html;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure><h4 id="3-ip绑定"><a class="header-anchor" href="#3-ip绑定">☀</a>3.<strong>ip绑定</strong></h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">upstream  mzd &#123;</span><br><span class="line">       server 127.0.0.1:8082 ;</span><br><span class="line">       server 127.0.0.1:8081 ;</span><br><span class="line">       ip_hash;</span><br><span class="line">    &#125;</span><br><span class="line">    server &#123;</span><br><span class="line">        listen       80;</span><br><span class="line">        server_name  www.tuesdayma.com;</span><br><span class="line">        location / &#123;</span><br><span class="line">            proxy_pass  http://mzd;</span><br><span class="line">            index  index.html index.htm;</span><br><span class="line">        &#125;</span><br><span class="line">        error_page   500 502 503 504  /50x.html;</span><br><span class="line">        location = /50x.html &#123;</span><br><span class="line">            root   html;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure><h3 id="5-配置超时"><a class="header-anchor" href="#5-配置超时">☀</a>5.配置超时</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen       80;</span><br><span class="line">    server_name  www.tuesdayma.com;</span><br><span class="line">    location / &#123;</span><br><span class="line">        proxy_pass  http://mzd;</span><br><span class="line">        proxy_connect_timeout 3s;</span><br><span class="line">        proxy_read_timeout 5s;</span><br><span class="line">        proxy_send_timeout 3s;</span><br><span class="line">        index  index.html index.htm;</span><br><span class="line">    &#125;</span><br><span class="line">    error_page   500 502 503 504  /50x.html;</span><br><span class="line">    location = /50x.html &#123;</span><br><span class="line">        root   html;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="6-Nginx匹配路径部分替换"><a class="header-anchor" href="#6-Nginx匹配路径部分替换">☀</a>6.Nginx匹配路径部分替换</h3><blockquote><p>nginx 配置 proxy_pass时可以实现URL路径的部分替换。</p><p>1.proxy_pass的目标地址，默认不带/，表示只代理域名，url和querystring部分不会变（把请求的path拼接到proxy_pass目标域名之后作为代理的URL）</p><p>2.如果在目标地址后增加/，则表示把path中location匹配成功的部分剪切掉之后再拼接到proxy_pass目标地址</p></blockquote><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    	access_log  /home/access.log;</span><br><span class="line">        error_log   /home/error.log;</span><br><span class="line">        server_name h5.xxx.com;</span><br><span class="line">        location  /abc &#123;</span><br><span class="line">                proxy_pass http://server_url;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">       location  /abc &#123;</span><br><span class="line">                proxy_pass http://server_url/;</span><br><span class="line">        &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure><blockquote><p>比如请求 /abc/b.html</p><p>如上两个匹配成功后，实际代理的目标url分别是</p><p>http://server_url/abc/b.html (把/abc/b.html拼接到http://server_url之后)</p><p>http://server_url/b.html (把/abc/b.html的/abc去掉之后，拼接到http://server_url/之后)</p></blockquote><h3 id="7-Nginx跨域设置"><a class="header-anchor" href="#7-Nginx跨域设置">☀</a>7.Nginx跨域设置</h3><p>解决场景：</p><p>静态资源服务器与nginx服务器不是同一台，nginx代理的服务器，未设置跨域。（简称非同源下的nginx代理）</p><p>同源下的代理就不需要设置跨域了。</p><p>个人理解可以解决，服务器端设置CORS的cookie缺陷。服务支持多源跨域访问，同时支持cookie。其实就是将服务器设置的CORS转移到代理服务器，变得灵活起来。</p><p><strong>一句话：同源下的代理需不要设置跨域，非同源下的代理需要设置跨域。</strong></p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span> <span class="string">&#x27;*&#x27;</span>;</span><br><span class="line"><span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Credentials&#x27;</span> <span class="string">&#x27;true&#x27;</span>;</span><br><span class="line"><span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Methods&#x27;</span> <span class="string">&#x27;GET,POST&#x27;</span>;</span><br></pre></td></tr></table></figure><p>生效的方案：</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">if</span> (<span class="variable">$request_method</span> = <span class="string">&#x27;OPTIONS&#x27;</span>) &#123;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span> <span class="string">&#x27;*&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Methods&#x27;</span> <span class="string">&#x27;GET, POST, OPTIONS&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span> <span class="string">&#x27;DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Max-Age&#x27;</span> <span class="number">1728000</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Content-Type&#x27;</span> <span class="string">&#x27;text/plain charset=UTF-8&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Content-Length&#x27;</span> <span class="number">0</span>;</span><br><span class="line">        <span class="attribute">return</span> <span class="number">204</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="attribute">if</span> (<span class="variable">$request_method</span> = <span class="string">&#x27;POST&#x27;</span>) &#123;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span> <span class="string">&#x27;*&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Methods&#x27;</span> <span class="string">&#x27;GET, POST, OPTIONS&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span> <span class="string">&#x27;DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#x27;</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="attribute">if</span> (<span class="variable">$request_method</span> = <span class="string">&#x27;GET&#x27;</span>) &#123;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span> <span class="string">&#x27;*&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Methods&#x27;</span> <span class="string">&#x27;GET, POST, OPTIONS&#x27;</span>;</span><br><span class="line">        <span class="attribute">add_header</span> <span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span> <span class="string">&#x27;DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#x27;</span>;</span><br><span class="line">       &#125;</span><br></pre></td></tr></table></figure><h2 id="10-常见报错"><a class="header-anchor" href="#10-常见报错">☀</a>10.常见报错</h2><ol><li><p>nginx -s reload 时报错</p><p>Nginx错误：nginx: [error] OpenEvent(“Global\ngx_reload_6252”) failed (2: The system cannot find the file specified)</p><p>错误原因：Nginx 尚未启动导致，执行 start nginx 命令开启Nginx</p></li><li><p>访问时报错<br> ginx1&quot; failed (123: The filename, directory name, or volume label syntax is incorrect), client: 127.0.0.1, server: 127.0.0.1, request: “GET / HTTP/1.0”, host: “tw”<br> 2020/10/28 21:34:14 [crit] 18412#15040: *26 CreateFile() &quot;D:\Data\Html</p><p>错误原因：配置root路径使用了 \</p></li></ol><h2 id="11-参考文献"><a class="header-anchor" href="#11-参考文献">☀</a>11.参考文献</h2><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.cnblogs.com/heioray/p/10329532.html">Nginx反向代理理解误区之proxy_cookie_domain</a></p><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.jb51.net/article/156230.htm">nginx配置教程之add_header的坑详解</a></p><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.jb51.net/article/156342.htm">Nginx跨域设置Access-Control-Allow-Origin无效的解决办法</a></p><div class="footer"><div class="copyright"><blockquote><p>博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</p><p>本文永久链接是：<a href="http://aner1001.gitee.io/posts/2021/05/13/fa918a90eed5/">http://aner1001.gitee.io/posts/2021/05/13/fa918a90eed5/</a></p></blockquote></div></div><div class="article-meta" id="bottom"><div class="new-meta-box"><div class="new-meta-item date" itemprop="dateUpdated" datetime="2021-05-13T20:20:37+08:00"><a class="notlink"><i class="fas fa-edit fa-fw" aria-hidden="true"></i><p>更新于：2021年5月13日</p></a></div><div class="new-meta-item meta-tags"><a class="tag" href="/tags/Nginx/" rel="nofollow"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i><p>Nginx</p></a></div><div class="new-meta-item share -mob-share-list"><div class="-mob-share-list share-body"> <a class="-mob-share-qq" title="" rel="external nofollow noopener noreferrer noopener" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://aner1001.gitee.io/posts/2021/05/13/fa918a90eed5/&title=Nginx学习笔记 - Tang's blog.&summary=Nginx主要用于1. 静态资源服务器，2. 反向代理，3. 负载均衡，本文主要围绕这三块进行描述。"><img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qq.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qq.png" srcset=""></a> <a class="-mob-share-qzone" title="" rel="external nofollow noopener noreferrer noopener" target="_blank" href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=http://aner1001.gitee.io/posts/2021/05/13/fa918a90eed5/&title=Nginx学习笔记 - Tang's blog.&summary=Nginx主要用于1. 静态资源服务器，2. 反向代理，3. 负载均衡，本文主要围绕这三块进行描述。"><img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qzone.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qzone.png" srcset=""></a> <a class="-mob-share-weibo" title="" rel="external nofollow noopener noreferrer noopener" target="_blank" href="http://service.weibo.com/share/share.php?url=http://aner1001.gitee.io/posts/2021/05/13/fa918a90eed5/&title=Nginx学习笔记 - Tang's blog.&summary=Nginx主要用于1. 静态资源服务器，2. 反向代理，3. 负载均衡，本文主要围绕这三块进行描述。"><img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/weibo.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/weibo.png" srcset=""></a><div class="hoverbox"> <a class="share"><img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/wechat.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/wechat.png" srcset=""></a><div class="target"> <img src=""></div></div> <a class="-mob-share-telegram" title="" rel="external nofollow noopener noreferrer noopener" target="_blank" href="https://t.me/share/url?url=http://aner1001.gitee.io/posts/2021/05/13/fa918a90eed5/&text=Nginx学习笔记 - Tang's blog."><img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/telegram.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/telegram.png" srcset=""></a></div></div></div></div><div class="prev-next"><a class="prev" href="/posts/2021/05/25/5acb08517bd6/"><p class="title"><i class="fas fa-chevron-left" aria-hidden="true"></i>Nodejs环境搭建</p><p class="content">本文介绍： nodejs环境的搭建。 nvm工具管理nodejs版本的使用。 npm包管理工具的使用以及命令。 ☀nodejs ☀概念： 什么是nodejs? 脚本语言需要一个解析器才能...</p></a><a class="next" href="/posts/2021/05/04/3c666189d167/"><p class="title">GoogleAnalytics<i class="fas fa-chevron-right" aria-hidden="true"></i></p><p class="content">记录谷歌分析工具的使用(✪ω✪)。 谷歌分析，即大家俗称的ga，全称google analytics，是谷歌推出的网站流量分析工具，可以说是当前业界最强大的流量分析工具，并且完全免费。 ☀...</p></a></div></article><article class="post white-box reveal shadow" id="comments"><p ct><i class="fas fa-comments"></i> 评论</p><div id="valine_container" class="valine_thread"><i class="fas fa-cog fa-spin fa-fw fa-2x"></i></div></article></div><aside class="l_side"><section class="widget toc-wrapper shadow desktop mobile" id="toc-div"><header><i class="fas fa-list fa-fw" aria-hidden="true"></i> <span class="name">本文目录</span></header><div class="content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-Nginx%E5%8A%9F%E8%83%BD%E4%BB%8B%E7%BB%8D"><span class="toc-text">1.Nginx功能介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E4%B8%8B%E8%BD%BD%E5%92%8C%E5%AE%89%E8%A3%85"><span class="toc-text">2.下载和安装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD"><span class="toc-text">下载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85"><span class="toc-text">安装</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87"><span class="toc-text">环境准备:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A5%E9%AA%A4"><span class="toc-text">步骤:</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E9%85%8D%E7%BD%AE%E4%BB%8B%E7%BB%8D"><span class="toc-text">3.配置介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#server"><span class="toc-text">server</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#listen"><span class="toc-text">listen</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#server-name"><span class="toc-text">server_name</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#location"><span class="toc-text">location</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#root"><span class="toc-text">root</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#index"><span class="toc-text">index</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-pass"><span class="toc-text">proxy_pass</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-redirect-off"><span class="toc-text">proxy_redirect off</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-set-header"><span class="toc-text">proxy_set_header</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-connect-timeout"><span class="toc-text">proxy_connect_timeout</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-send-timeout"><span class="toc-text">proxy_send_timeout</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-read-timeout"><span class="toc-text">proxy_read_timeout</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-cookie-path"><span class="toc-text">proxy_cookie_path</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#proxy-cookie-domain"><span class="toc-text">proxy_cookie_domain</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#add-header"><span class="toc-text">add_header</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#rewrite-TODO"><span class="toc-text">rewrite &#x2F;&#x2F;TODO</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#return"><span class="toc-text">return</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#default-type"><span class="toc-text">default_type</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#upstram-XXX"><span class="toc-text">upstram XXX{}</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#server-2"><span class="toc-text">server</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ip-hash"><span class="toc-text">ip_hash</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-text">4.反向代理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-text">5.动静分离</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5"><span class="toc-text">概念</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%EF%BC%9A"><span class="toc-text">静态资源：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E8%B5%84%E6%BA%90%EF%BC%9A"><span class="toc-text">动态资源：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB%EF%BC%9A"><span class="toc-text">动静分离：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BC%AA%E9%9D%99%E6%80%81"><span class="toc-text">伪静态</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB%E5%8E%9F%E5%9B%A0%EF%BC%9A"><span class="toc-text">动静分离原因：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%96%B9%E6%B3%95%EF%BC%9A"><span class="toc-text">实现动静分离的两种方法：</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E4%B8%8D%E5%90%8C%E5%9F%9F%E5%90%8D%E6%9D%A5%E6%8B%A6%E6%88%AA"><span class="toc-text">通过不同域名来拦截</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#location%E6%9D%A5%E6%8B%A6%E6%88%AA"><span class="toc-text">location来拦截</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-text">6.负载均衡</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E5%9B%A0%E4%B8%8E%E4%BD%9C%E7%94%A8%EF%BC%9A"><span class="toc-text">原因与作用：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E7%A7%8D%E5%9F%BA%E6%9C%AC%E7%9A%84%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%AE%97%E6%B3%95%EF%BC%9A"><span class="toc-text">三种基本的负载均衡算法：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-%E6%95%85%E9%9A%9C%E8%BD%AC%E7%A7%BB%E6%9C%BA%E5%88%B6"><span class="toc-text">7.故障转移机制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E5%91%BD%E4%BB%A4"><span class="toc-text">8.命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Windows"><span class="toc-text">Windows</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Linux"><span class="toc-text">Linux</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-%E9%85%8D%E7%BD%AE%E6%A1%88%E4%BE%8B%EF%BC%9A"><span class="toc-text">9.配置案例：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="toc-text">1.静态资源服务器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#root%E7%9B%B8%E5%AF%B9%E8%B7%AF%E5%BE%84"><span class="toc-text">root相对路径</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%9F%E5%90%8D%E5%8C%B9%E9%85%8D%E8%AE%BF%E9%97%AE%E4%B8%8D%E5%90%8C%E8%B5%84%E6%BA%90"><span class="toc-text">域名匹配访问不同资源</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-text">2.反向代理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-text">3.动静分离</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E4%B8%8D%E5%90%8C%E5%9F%9F%E5%90%8D%E6%9D%A5%E6%8B%A6%E6%88%AA-2"><span class="toc-text">通过不同域名来拦截</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-text">4.负载均衡</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E8%BD%AE%E8%AF%A2"><span class="toc-text">1.轮询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E6%9D%83%E9%87%8D"><span class="toc-text">2.权重</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-ip%E7%BB%91%E5%AE%9A"><span class="toc-text">3.ip绑定</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E9%85%8D%E7%BD%AE%E8%B6%85%E6%97%B6"><span class="toc-text">5.配置超时</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-Nginx%E5%8C%B9%E9%85%8D%E8%B7%AF%E5%BE%84%E9%83%A8%E5%88%86%E6%9B%BF%E6%8D%A2"><span class="toc-text">6.Nginx匹配路径部分替换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-Nginx%E8%B7%A8%E5%9F%9F%E8%AE%BE%E7%BD%AE"><span class="toc-text">7.Nginx跨域设置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-%E5%B8%B8%E8%A7%81%E6%8A%A5%E9%94%99"><span class="toc-text">10.常见报错</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="toc-text">11.参考文献</span></a></li></ol></div></section></aside><script>window.pdata={},pdata.ispage=!0,pdata.postTitle="Nginx学习笔记",pdata.commentPath="",pdata.commentPlaceholder="";var l_header=document.getElementById("l_header");l_header.classList.add("show");var cover_wrapper=document.querySelector(".cover-wrapper");cover_wrapper.id="none",cover_wrapper.style.display="none"</script></div><footer class="footer clearfix"><br><br><div class="aplayer-container"><meting-js theme="#1BCDFC" autoplay="false" volume="0.7" loop="all" order="list" fixed="false" list-max-height="320px" server="netease" type="playlist" id="5472211102" list-folded="true"></meting-js></div><br><div class="social-wrapper"><a href="/atom.xml" class="social fas fa-rss flat-btn" target="_blank" rel="external nofollow noopener noreferrer"></a><a href="mailto:584856024@qq.com" class="social fas fa-envelope flat-btn" target="_blank" rel="external nofollow noopener noreferrer"></a><a href="https://github.com/aner1001" class="social fab fa-github flat-btn" target="_blank" rel="external nofollow noopener noreferrer"></a><a href="https://music.163.com/#/user/home?id=1402271985" class="social fas fa-headphones-alt flat-btn" target="_blank" rel="external nofollow noopener noreferrer"></a></div><div><p>博客内容遵循 <a target="_blank" rel="noopener external nofollow noreferrer" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</a></p></div><div><p><span id="lc-sv">本站总访问量为<span id="number"><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span> 次</span> <span id="lc-uv">访客数为<span id="number"><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span> 人</span></p></div> 本站使用 <a href="https://github.com/volantis-x/hexo-theme-volantis/tree/4.3.1" rel="external nofollow noreferrer" target="_blank" class="codename">Volantis</a> 作为主题，总访问量为<span id="busuanzi_value_site_pv"><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span> 次<div class="copyright"><p><a href="/">Copyright © 2021-2021 XXX</a></p></div></footer><a id="s-top" class="fas fa-arrow-up fa-fw" href="javascript:void(0)" rel="external nofollow noreferrer"></a></div></div><div><script>function VPjax(){function e(e,t){this.name=t||e.name,this.run=()=>{e()}}this.list=[],this.start=()=>{for(var e=0;e<this.list.length;e++)this.list[e].run()},this.push=(t,n)=>{var i=new e(t,n);this.list.push(i)}}function loadScript(e,t){setTimeout((function(){var n=document.getElementsByTagName("head")[0]||document.documentElement,i=document.createElement("script");i.setAttribute("type","text/javascript"),t&&(i.onload=t),i.setAttribute("src",e),n.appendChild(i)}))}window.volantis={},window.volantis.loadcss=document.getElementById("loadcss"),volantis.pjax={},volantis.pjax.method={complete:new VPjax,error:new VPjax,send:new VPjax},volantis.pjax={...volantis.pjax,push:volantis.pjax.method.complete.push,error:volantis.pjax.method.error.push,send:volantis.pjax.method.send.push};var loadCSS=function(e,t,n,i){var o,a=window.document,s=a.createElement("link");if(t)o=t;else{var r=(a.body||a.getElementsByTagName("head")[0]).childNodes;o=r[r.length-1]}var d=a.styleSheets;if(i)for(var l in i)i.hasOwnProperty(l)&&s.setAttribute(l,i[l]);s.rel="stylesheet",s.href=e,s.media="only x",function e(t){if(a.body)return t();setTimeout((function(){e(t)}))}((function(){o.parentNode.insertBefore(s,t?o:o.nextSibling)}));var u=function(e){for(var t=s.href,n=d.length;n--;)if(d[n].href===t)return e();setTimeout((function(){u(e)}))};function h(){s.addEventListener&&s.removeEventListener("load",h),s.media=n||"all"}return s.addEventListener&&s.addEventListener("load",h),s.onloadcssdefined=u,u(h),s}</script><script>loadCSS("https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14/css/all.min.css",window.volantis.loadcss),loadCSS("https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css",window.volantis.loadcss),loadCSS("https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.css",window.volantis.loadcss)</script><script src="https://cdn.jsdelivr.net/npm/jquery@3.5/dist/jquery.min.js"></script><script>function pjax_fancybox(){$(".md .gallery").find("img").each((function(){var a=document.createElement("a");$(a).attr("class","fancybox"),$(a).attr("pjax-fancybox",""),$(a).attr("href",$(this).attr("src")),$(this).attr("data-original")&&$(a).attr("href",$(this).attr("data-original")),$(a).attr("data-fancybox","images");var t="";$(this).attr("alt")&&($(a).attr("data-caption",$(this).attr("alt")),t=$(this).attr("alt"));var n=document.createElement("div");$(n).addClass("fancybox"),$(this).wrap(n);var o=document.createElement("span");$(o).addClass("image-caption"),$(o).text(t),$(this).after(o),$(this).wrap(a)})),$(".md .gallery").find("img").fancybox({selector:'[data-fancybox="images"]',hash:!1,loop:!1,closeClick:!0,helpers:{overlay:{closeClick:!0}},buttons:["zoom","close"]})}function SCload_fancybox(){0!=$(".md .gallery").find("img").length&&(loadCSS("https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css",document.getElementById("loadcss")),loadScript("https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js",pjax_fancybox))}function Pjax_SCload_fancybox(){void 0===$.fancybox?SCload_fancybox():pjax_fancybox()}$((function(){SCload_fancybox()})),volantis.pjax.push(Pjax_SCload_fancybox),volantis.pjax.send(()=>{void 0!==$.fancybox&&$.fancybox.close()},"fancybox")</script><script src="https://cdn.jsdelivr.net/npm/jquery-backstretch@2.1.18/jquery.backstretch.min.js"></script><script type="text/javascript">var imgs=["https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/001.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/002.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/003.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/004.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/005.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/006.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/012.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/016.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/019.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/025.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/033.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/034.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/035.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/038.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/039.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/042.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/046.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/051.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/052.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/054.jpg","https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/056.jpg"];{function shuffle(t){for(var n=t.length;n--;){var i=Math.floor(Math.random()*n),l=t[i];t[i]=t[n],t[n]=l}}shuffle(imgs)}function Pjax_backstretch(){$("#cover-backstretch").backstretch(imgs,{duration:"10000",fade:"1500"})}loadScript("https://cdn.jsdelivr.net/npm/jquery-backstretch@2.1.18/jquery.backstretch.min.js",Pjax_backstretch)</script><div id="rightmenu-wrapper"><ul class="list-v rightmenu" id="rightmenu-content"><li class="option"><a class="vlts-menu opt fix-cursor-default" id="menu-copy-text" onclick='document.execCommand("copy")'><i class="fa fa-copy fa-fw"></i> 复制文本</a><hr id="hr-text"><a class="vlts-menu opt fix-cursor-default" id="menu-copy-href"><i class="fa fa-link fa-fw"></i> 复制链接</a><a class="vlts-menu opt fix-cursor-default" id="menu-open-href"><i class="fa fa-external-link-square-alt fa-fw"></i> 在新标签页打开</a><hr id="hr-href"><a class="vlts-menu opt fix-cursor-default" id="menu-copy-src"><i class="fa fa-image fa-fw"></i> 复制图片地址</a><hr id="hr-src"></li><li class="navigation"><a class="nav icon-only fix-cursor-default" onclick="history.back()"><i class="fa fa-arrow-left fa-fw"></i></a><a class="nav icon-only fix-cursor-default" onclick="history.forward()"><i class="fa fa-arrow-right fa-fw"></i></a><a class="nav icon-only fix-cursor-default" onclick="window.location.reload()"><i class="fa fa-redo fa-fw"></i></a><a class="nav icon-only fix-cursor-default" href="/"><i class="fa fa-home fa-fw"></i></a></li><hr><li><a class="vlts-menu fix-cursor-default" href="https://volantis.js.org/faqs/" id="https:volantisjsorgfaqs"><i class="fa fa-question fa-fw"></i> 常见问题</a></li><li><a class="vlts-menu fix-cursor-default" href="https://volantis.js.org/examples/" id="https:volantisjsorgexamples"><i class="fa fa-rss fa-fw"></i> 示例博客</a></li><li><a class="vlts-menu fix-cursor-default" href="https://volantis.js.org/contributors/" id="https:volantisjsorgcontributors"><i class="fa fa-fan fa-spin fa-fw"></i> 加入社区</a></li><hr><li><a class="vlts-menu fix-cursor-default" href="https://github.com/volantis-x/volantis-docs/" id="https:githubcomvolantis-xvolantis-docs"><i class="fa fa-code-branch fa-fw"></i> 本站源码</a></li><li><a class="vlts-menu fix-cursor-default" href="https://github.com/volantis-x/hexo-theme-volantis/" id="https:githubcomvolantis-xhexo-theme-volantis"><i class="fa fa-code-branch fa-fw"></i> 主题源码</a></li><hr><li><a class="vlts-menu fix-cursor-default" onclick='document.execCommand("print")'><i class="fa fa-print fa-fw"></i> 打印页面</a></li><hr><li><a class="vlts-menu fix-cursor-default toggle-mode-btn"><i class="fas fa-moon fa-fw"></i> Dark</a></li><hr><li class="music name"><p class="nav music-title fix-cursor-default"></p></li><li class="music ctrl"><a class="nav icon-only backward fix-cursor-default" onclick="aplayerBackward()"><i class="fa fa-step-backward fa-fw"></i></a><a class="nav icon-only toggle fix-cursor-default" onclick="aplayerToggle()"><i class="fa fa-play fa-fw"></i></a><a class="nav icon-only forward fix-cursor-default" onclick="aplayerForward()"><i class="fa fa-step-forward fa-fw"></i></a></li><li class="music volume"><a class="nav volume"><div class="aplayer-volume-bar-wrap"><div class="aplayer-volume-bar fix-cursor-pointer"><div class="aplayer-volume"></div><i class="left fa fa-volume-off fa-fw"></i><i class="right fa fa-volume-up fa-fw"></i></div></div></a></li></ul></div><script>function popMenu(e){var t=document.getElementById("rightmenu-wrapper"),n=document.getElementById("rightmenu-content"),l=document.documentElement.clientWidth||document.body.clientWidth,o=document.documentElement.clientHeight||document.body.clientHeight;t.style.left=e.clientX+"px",t.style.top=e.clientY+"px",t.style.display="block",2*e.clientX>l?n.classList.add("left"):n.classList.remove("left"),2*e.clientY>o?n.classList.add("top"):n.classList.remove("top");let c=document.getElementById("hr-text"),d=document.getElementById("hr-src"),y=document.getElementById("hr-href"),s=document.getElementById("menu-copy-src");null!=s&&(e.target.currentSrc?(s.style.display="block",s.addEventListener("click",(function(t){copyString(e.target.currentSrc)}),{once:!0}),d.style.display="block"):(s.style.display="none",d.style.display="none"));let r="";if(e.path)for(i=0;i<e.path.length;i++)null!=e.path[i].href&&e.path[i].href.length>0&&(r=e.path[i].href);let u=document.getElementById("menu-copy-text");u.style.display="none",c.style.display="none",0==r.length&&window.getSelection().toString()&&(u.style.display="block",c.style.display="block");let m=document.getElementById("menu-open-href");null!=m&&(r.length>0?(m.style.display="block",m.addEventListener("click",(function(e){window.open(r)}),{once:!0}),y.style.display="block"):(m.style.display="none",y.style.display="none"));let a=document.getElementById("menu-copy-href");return null!=a&&(r.length>0?(a.style.display="block",a.addEventListener("click",(function(e){copyString(r)}),{once:!0})):a.style.display="none"),volantis.APlayerLoaded&&checkAPlayer(),!1}function hideMenu(){document.getElementById("rightmenu-wrapper").style.display="none"}function copyString(e){const t=document.createElement("input");t.setAttribute("readonly","readonly"),document.body.appendChild(t),t.setAttribute("value",e),t.select(),document.execCommand("copy"),document.body.removeChild(t)}window.document.oncontextmenu=function(e){return!!e.ctrlKey||(!!/Android|webOS|BlackBerry/i.test(navigator.userAgent)||popMenu(e))},document.addEventListener("click",(function(e){document.getElementById("rightmenu-wrapper").style.display="none"})),document.execCommand("click")</script><script>function loadIssuesJS(){0!=$(".md").find(".issues-api").length&&loadScript("https://cdn.jsdelivr.net/npm/hexo-theme-volantis@4.3.1/source/js/issues.min.js")}$((function(){loadIssuesJS()})),volantis.pjax.push(()=>{"undefined"==typeof IssuesAPI&&loadIssuesJS()},"IssuesJS")</script><script defer="defer" src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js"></script><script>window.lazyLoadOptions={elements_selector:".lazyload",threshold:0},window.addEventListener("LazyLoad::Initialized",(function(n){window.lazyLoadInstance=n.detail.instance}),!1),document.addEventListener("DOMContentLoaded",(function(){lazyLoadInstance.update()})),document.addEventListener("pjax:complete",(function(){lazyLoadInstance.update()}))</script><script>window.FPConfig={delay:0,ignoreKeywords:[],maxRPS:5,hoverDelay:25}</script><script defer="defer" src="https://cdn.jsdelivr.net/gh/gijo-varghese/flying-pages@2.1.2/flying-pages.min.js"></script><script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script><script>var clipboard=new ClipboardJS(".btn-copy",{target:function(e){return e.nextElementSibling}});function wait(e,i){window.setTimeout(e,i)}function pjax_initCopyCode(){if($(".highlight .code pre").length+$(".article pre code").length!=0){var e="";e+='<button class="btn-copy" data-clipboard-snippet="">',e+='<i class="fas fa-copy"></i><span>COPY</span>',e+="</button>",$(".highlight .code pre").before(e),$(".article pre code").before(e),clipboard.off("success").on("success",(function(e){let i=$(e.trigger);i.addClass("copied");let a=$(i.find("i"));a.removeClass("fa-copy"),a.addClass("fa-check-circle");let n=$(i.find("span"));n[0].innerText="COPIED",wait((function(){a.removeClass("fa-check-circle"),a.addClass("fa-copy"),n[0].innerText="COPY"}),2e3)})),clipboard.off("error").on("error",(function(e){e.clearSelection();let i=$(e.trigger);i.addClass("copy-failed");let a=$(i.find("i"));a.removeClass("fa-copy"),a.addClass("fa-times-circle");let n=$(i.find("span"));n[0].innerText="COPY FAILED",wait((function(){a.removeClass("fa-times-circle"),a.addClass("fa-copy"),n[0].innerText="COPY"}),2e3)}))}}$((function(){pjax_initCopyCode()})),volantis.pjax.push(pjax_initCopyCode)</script><script type="text/javascript">function pjax_scrollrebeal(){ScrollReveal().reveal(".l_main .reveal",{distance:"32px",duration:"800",interval:"20",scale:"1",easing:"ease-out"})}loadScript("https://cdn.jsdelivr.net/npm/scrollreveal@4.0.6/dist/scrollreveal.min.js"),$((function(){var e=setInterval((function(){"block"==$("#safearea").css("display")&&"undefined"!=typeof ScrollReveal&&(clearInterval(e),pjax_scrollrebeal())}),100)})),volantis.pjax.push(pjax_scrollrebeal)</script><script>let APlayerController=new Object;APlayerController.id="5472211102",APlayerController.volume="0.7",loadCSS("https://cdn.jsdelivr.net/npm/aplayer@1.10/dist/APlayer.min.css",window.volantis.loadcss),loadScript("https://cdn.jsdelivr.net/npm/aplayer@1.10/dist/APlayer.min.js"),window.volantis.APlayerLoaded=0;var checkAPlayer=setInterval((function(){window.APlayer&&"block"==$("#safearea").css("display")&&(clearInterval(checkAPlayer),window.volantis.APlayerLoaded||window.MetingJSElement||(window.volantis.APlayerLoaded=1,loadScript("https://cdn.jsdelivr.net/npm/meting@2.0/dist/Meting.min.js")))}),2500);loadScript("https://cdn.jsdelivr.net/npm/hexo-theme-volantis@4.3.1/source/js/aplayer.min.js")</script><script src="https://cdn.jsdelivr.net/npm/hexo-theme-volantis@4.3.1/source/js/valine.min.js"></script><script>function emoji(i,e,a){return i+"/"+i+"-"+e+"."+a}for(var emojiMaps={},i=1;i<=54;i++)emojiMaps["tieba-"+i]=emoji("tieba",i,"png");for(i=1;i<=101;i++)emojiMaps["qq-"+i]=emoji("qq",i,"gif");for(i=1;i<=116;i++)emojiMaps["aru-"+i]=emoji("aru",i,"gif");for(i=1;i<=125;i++)emojiMaps["twemoji-"+i]=emoji("twemoji",i,"png");for(i=1;i<=4;i++)emojiMaps["weibo-"+i]=emoji("weibo",i,"png");function pjax_valine(){if(!document.querySelectorAll("#valine_container")[0])return;let i=pdata.commentPlaceholder||"快来评论吧~",e=pdata.commentPath;if(0==e.length){e=""||decodeURI(window.location.pathname)}(new Valine).init(Object.assign({path:null,placeholder:"快来评论吧~",appId:"eL9fCmXLl3nAe225XYo1cRxj-MdYXbMMI",appKey:"5uTd5Uu8WJJIej6Efrivsvaa",meta:["nick","mail","link"],requiredFields:["nick","mail"],enableQQ:!0,recordIP:!1,avatar:"robohash",pageSize:10,lang:"zh-cn",highlight:!0,mathJax:!1},{el:"#valine_container",path:e,placeholder:i,emojiCDN:"https://cdn.jsdelivr.net/gh/volantis-x/cdn-emoji/valine/",emojiMaps:emojiMaps}))}$((function(){pjax_valine()})),volantis.pjax.push(pjax_valine)</script><script defer="defer" src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-busuanzi@2.3/js/busuanzi.pure.mini.js" data-pjax></script><script src="https://cdn.jsdelivr.net/npm/hexo-theme-volantis@4.3.1/source/js/app.min.js"></script><script>const SearchServiceimagePath="https://cdn.jsdelivr.net/gh/volantis-x/cdn-volantis@master/img/",ROOT="/".endsWith("/")?"/":"//";function listenSearch(){customSearch=new HexoSearch({imagePath:SearchServiceimagePath})}function setSearchService(){listenSearch()}$(".input.u-search-input").one("focus",(function(){loadScript("https://cdn.jsdelivr.net/gh/volantis-x/cdn-volantis@2.6.4/js/search.js",setSearchService)}))</script><script src="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.js"></script><script type="text/javascript">$((function(){Waves.attach(".flat-btn",["waves-button"]),Waves.attach(".float-btn",["waves-button","waves-float"]),Waves.attach(".float-btn-light",["waves-button","waves-float","waves-light"]),Waves.attach(".flat-box",["waves-block"]),Waves.attach(".float-box",["waves-block","waves-float"]),Waves.attach(".waves-image"),Waves.init()}))</script><script src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-volantis@2/js/comment_typing.js"></script><script defer="defer">const LCCounter={app_id:"C2dIVXJ48qC7eHgvQRId6niR-MdYXbMMI",app_key:"1956OGJ4LQhDxheBAO5WhPJn",custom_api_server:"",getRecord:(e,t,r)=>new Promise((function(n,i){e("get","/classes/Counter?where="+encodeURIComponent(JSON.stringify({url:t}))).then(e=>e.json()).then(({results:o,code:s,error:a})=>{if(401===s)throw a;if(o&&o.length>0){var l=o[0];n(l)}else e("post","/classes/Counter",{url:t,title:r,times:0}).then(e=>e.json()).then((e,t)=>{if(t)throw t;n(e)}).catch(e=>{console.error("Failed to create",e),i(e)})}).catch(e=>{console.error("LeanCloud Counter Error:",e),i(e)})})),increment:(e,t)=>new Promise((function(r,n){e("post","/batch",{requests:t}).then(e=>{if((e=e.json()).error)throw e.error;r(e)}).catch(e=>{console.error("Failed to save visitor count",e),n(e)})})),buildIncrement:e=>({method:"PUT",path:"/1.1/classes/Counter/"+e,body:{times:{__op:"Increment",amount:1}}}),validUV(){var e="LeanCloudUVTimestamp",t=localStorage.getItem(e);return!(t&&(new Date).getTime()-parseInt(t)<=864e5)&&(localStorage.setItem(e,(new Date).getTime().toString()),!0)},addCount(e){var t=[],r=[],n=document.querySelector("#lc-sv"),o=this.getRecord(e,"http://aner1001.gitee.io/#lc-sv","Visits").then(e=>{r.push(this.buildIncrement(e.objectId));var t=document.querySelectorAll("#lc-sv #number");t.length>0&&t.forEach((t,r,i)=>{t.innerText=e.times+1,n&&(n.style.display="inline")})});t.push(o);var s=document.querySelector("#lc-uv"),a=this.getRecord(e,"http://aner1001.gitee.io/#lc-uv","Visitors").then(e=>{var t=this.validUV();t&&r.push(this.buildIncrement(e.objectId));var n=document.querySelectorAll("#lc-uv #number");n.length>0&&n.forEach((r,n,i)=>{r.innerText=e.times+(t?1:0),s&&(s.style.display="inline")})});t.push(a);var l=document.querySelectorAll("#lc-pv");for(l.length,i=0;i<l.length;i++){let n=l[i],o=n.getAttribute("data-title");var h="http://aner1001.gitee.io"+n.getAttribute("data-path");if(h){var c=this.getRecord(e,h,o).then(e=>{let t=window.location.pathname;if(t.includes("index.html")&&(t=t.substring(0,t.lastIndexOf("index.html"))),n.getAttribute("data-path")==t&&r.push(this.buildIncrement(e.objectId)),n){var i=n.querySelector("#lc-pv #number");i&&(n.getAttribute("data-path")==t?i.innerText=(e.times||0)+1:i.innerText=e.times||0,n.style.display="inline")}});t.push(c)}}Promise.all(t).then(()=>{r.length>0&&this.increment(e,r)})},fetchData(e){this.addCount((t,r,n)=>fetch(`${e}/1.1${r}`,{method:t,headers:{"X-LC-Id":this.app_id,"X-LC-Key":this.app_key,"Content-Type":"application/json"},body:JSON.stringify(n)}))},refreshCounter(){var e="-MdYXbMMI"!==this.app_id.slice(-9)?this.custom_api_server:`https://${this.app_id.slice(0,8).toLowerCase()}.api.lncldglobal.com`;e?this.fetchData(e):fetch("https://app-router.leancloud.cn/2/route?appId="+this.app_id).then(e=>e.json()).then(({api_server:e})=>{this.fetchData("https://"+e)})}};LCCounter.refreshCounter(),document.addEventListener("pjax:complete",(function(){LCCounter.refreshCounter()}))</script><script>const rootElement=document.documentElement,darkModeStorageKey="user-color-scheme",rootElementDarkModeAttributeName="data-user-color-scheme",setLS=(e,t)=>{localStorage.setItem(e,t)},removeLS=e=>{localStorage.removeItem(e)},getLS=e=>localStorage.getItem(e),getModeFromCSSMediaQuery=()=>window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",resetRootDarkModeAttributeAndLS=()=>{var e;rootElement.removeAttribute("data-user-color-scheme"),e=darkModeStorageKey,localStorage.removeItem(e)},validColorModeKeys={dark:!0,light:!0},applyCustomDarkModeSettings=e=>{const t=e||getLS(darkModeStorageKey);t===getModeFromCSSMediaQuery()?resetRootDarkModeAttributeAndLS():validColorModeKeys[t]?rootElement.setAttribute("data-user-color-scheme",t):resetRootDarkModeAttributeAndLS()},invertDarkModeObj={dark:"light",light:"dark"},toggleCustomDarkMode=()=>{let e=getLS(darkModeStorageKey);if(validColorModeKeys[e])e=invertDarkModeObj[e];else{if(null!==e)return;e=invertDarkModeObj[getModeFromCSSMediaQuery()]}var t,o;return t=darkModeStorageKey,o=e,localStorage.setItem(t,o),e};var btn=$("#wrapper .toggle-mode-btn,#rightmenu-wrapper .toggle-mode-btn");function bindToggleButton(){btn.on("click",e=>{const t=toggleCustomDarkMode();applyCustomDarkModeSettings(t)})}applyCustomDarkModeSettings(),document.addEventListener("DOMContentLoaded",bindToggleButton),volantis.pjax.push(bindToggleButton),volantis.pjax.send(()=>{btn.unbind("click")},"toggle-mode-btn-unbind")</script><script>function listennSidebarTOC(){const e=document.querySelectorAll(".toc li");if(!e.length)return;const t=[...e].map(e=>{const t=e.querySelector(".toc-link"),n=document.getElementById(decodeURI(t.getAttribute("href")).replace("#",""));return t.addEventListener("click",e=>{e.preventDefault(),window.scrollTo({top:n.offsetTop+100,behavior:"smooth"})}),n});!function n(o){o=Math.floor(o+1e4);let r=new IntersectionObserver((r,c)=>{let i=document.documentElement.scrollHeight+100;if(i>o)return c.disconnect(),void n(i);let l=function(e){let n=0,o=e[n];if(o.boundingClientRect.top>0)return n=t.indexOf(o.target),0===n?0:n-1;for(;n<e.length;n++){if(!(e[n].boundingClientRect.top<=0))return t.indexOf(o.target);o=e[n]}return t.indexOf(o.target)}(r);!function(e){if(e.classList.contains("active-current"))return;document.querySelectorAll(".toc .active").forEach(e=>{e.classList.remove("active","active-current")}),e.classList.add("active","active-current");let t=e.parentNode;for(;!t.matches(".toc");)t.matches("li")&&t.classList.add("active"),t=t.parentNode}(e[l])},{rootMargin:o+"px 0px -100% 0px",threshold:0});t.forEach(e=>{e&&r.observe(e)})}(document.documentElement.scrollHeight)}document.addEventListener("DOMContentLoaded",listennSidebarTOC),document.addEventListener("pjax:success",listennSidebarTOC)</script><script>"serviceWorker"in navigator&&navigator.serviceWorker.register("/sw.js").then((function(e){e.onupdatefound=function(){var r=e.installing;r.onstatechange=function(){switch(r.state){case"installed":navigator.serviceWorker.controller?console.log("Updated serviceWorker."):console.log("serviceWorker Sucess!");break;case"redundant":console.log("The installing service worker became redundant.")}}}})).catch((function(e){console.log("Error during service worker registration:",e)}))</script><script src="https://cdn.jsdelivr.net/npm/pjax@0.2.8/pjax.min.js"></script><div class="pjax-animate"><script src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js"></script><div id="loading-bar-wrapper"><script>NProgress.configure({parent:"#loading-bar-wrapper",trickleSpeed:100})</script></div><script>window.ShowLoading=function(){NProgress.start()},window.HideLoading=function(){NProgress.done()}</script><script>volantis.pjax.push(window.HideLoading,"HideLoading"),volantis.pjax.send(window.ShowLoading,"ShowLoading"),volantis.pjax.error(window.HideLoading,"HideLoading")</script></div><script>var pjax;document.addEventListener("DOMContentLoaded",(function(){pjax=new Pjax({elements:'a[href]:not([href^="#"]):not([href="javascript:void(0)"]):not([pjax-fancybox])',selectors:["title","#pjax-container","#pjax-header-nav-list"],cacheBust:!1,timeout:5e3})})),document.addEventListener("pjax:send",(function(e){try{var n=window.location.pathname,t=e.triggerElement.href,o=[""];""!=o[0]&&o.forEach(e=>{-1==n.indexOf(e)&&-1==t.indexOf(e)||(window.location.href=t)})}catch(e){}window.subData=null,volantis.$switcher.removeClass("active"),volantis.$header.removeClass("z_search-open"),volantis.$wrapper.removeClass("sub"),volantis.$topBtn.unbind("click"),$(".menu a").unbind("click"),$(window).unbind("resize"),$(window).unbind("scroll"),$(document).unbind("scroll"),$(document).unbind("click"),$("body").unbind("click"),volantis.pjax.method.send.start()})),document.addEventListener("pjax:complete",(function(){$(".nav-main").find(".list-v").not(".menu-phone").removeAttr("style",""),$(".menu-phone.list-v").removeAttr("style",""),$("script[data-pjax], .pjax-reload script").each((function(){$(this).parent().append($(this).remove())}));try{volantis.pjax.method.complete.start()}catch(e){console.log(e)}})),document.addEventListener("pjax:error",(function(e){volantis.pjax.method.error.start(),window.location.href=e.triggerElement.href}))</script></div></body></html>